package io.github.wanggit.antrpc.commons.zookeeper;

import org.apache.curator.utils.ZKPaths;

import java.util.List;

public abstract class ZkNodeType {

    private ZkNodeType() {
        throw new IllegalStateException();
    }

    /** 注册服务节点的类型 */
    public enum RegisterNodeType {
        ROOT,
        IP,
        INTERFACE
    }

    /** 负载均衡节点的类型 */
    public enum LbNodeType {
        ROOT,
        INTERFACE
    }

    /** 服务注销节点的类型 */
    public enum UnregNodeType {
        ROOT,
        IP,
        INTERFACE
    }

    /** 控制中心注册配置的类型 */
    public enum ConfigNodeType {
        ROOT,
        ENV,
        SPACE,
        KEY
    }

    /** 订阅配置的类型 */
    public enum SubscribeConfigNodeType {
        KEY
    }

    /** 订阅服务的类型 */
    public enum SubscribeNodeType {
        ROOT,
        APP_NAME_IP_PORT,
        INTERFACE
    }

    public static SubscribeNodeType getSubscribeNodeType(String path) {
        List<String> items = ZKPaths.split(path);
        int level = items.size();
        if (level == 1) {
            return SubscribeNodeType.ROOT;
        } else if (level == 2) {
            return SubscribeNodeType.APP_NAME_IP_PORT;
        } else if (level == 3) {
            return SubscribeNodeType.INTERFACE;
        } else {
            return null;
        }
    }

    public static SubscribeConfigNodeType getSubscribeConfigNodeType(String path) {
        List<String> items = ZKPaths.split(path);
        int level = items.size();
        if (level == 4) {
            return SubscribeConfigNodeType.KEY;
        }
        return null;
    }

    public static ConfigNodeType getConfigNodeType(String path) {
        List<String> items = ZKPaths.split(path);
        int level = items.size();
        if (level == 1) {
            return ConfigNodeType.ROOT;
        } else if (level == 2) {
            return ConfigNodeType.ENV;
        } else if (level == 3) {
            return ConfigNodeType.SPACE;
        } else if (level == 4) {
            return ConfigNodeType.KEY;
        }
        return null;
    }

    public static RegisterNodeType getRegisterNodeType(String path) {
        int level = ZKPaths.split(path).size();
        if (level == 1) {
            return RegisterNodeType.ROOT;
        } else if (level == 2) {
            return RegisterNodeType.IP;
        } else if (level == 3) {
            return RegisterNodeType.INTERFACE;
        }
        return null;
    }

    public static UnregNodeType getUnregNodeType(String path) {
        int level = ZKPaths.split(path).size();
        if (level == 1) {
            return UnregNodeType.ROOT;
        } else if (level == 2) {
            return UnregNodeType.IP;
        } else if (level == 3) {
            return UnregNodeType.INTERFACE;
        }
        return null;
    }

    public static LbNodeType getLbNodeType(String path) {
        int level = ZKPaths.split(path).size();
        if (level == 1) {
            return LbNodeType.ROOT;
        } else if (level == 2) {
            return LbNodeType.INTERFACE;
        }
        return null;
    }
}
